package com.andnnl.proxy;

import java.lang.reflect.Proxy;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by chenss on 2019/12/19.
 */
public class JavaDynamicProxyTest {
    public static void main(String[] args) {
        Developer jack=new JavaDeveloper("jack");
        Developer jack2=new JavaDeveloper("jack2");
        AtomicInteger i= new AtomicInteger();
        Developer developer= (Developer) Proxy.newProxyInstance(jack.getClass().getClassLoader(),
                jack.getClass().getInterfaces(),(proxy,method,arg)->{
//                    if(method.getName().equals("code")){
//                        System.out.println("invoke coding...");
//                        return method.invoke(jack,arg);
//                    }
//                    if(method.getName().equals("debug")){
//                        System.out.println("no bug...");
//                        return null;
//                    }
//                    return null;
                    System.out.println("Before method invocation");
                    Developer d= (i.getAndIncrement()) %2==0?jack:jack2;
                    Object result = method.invoke(d, args);
                    System.out.println("After method invocation");
                    return result;
                });


        developer.code();
        developer.code();
        developer.debug();
        developer.debug();
    }
}
